package org.mygui;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

import org.mygui.model.ObjectTableModel;
import org.mygui.mongo.MyMongoDBClient;

import java.awt.*;  
import java.awt.event.ActionEvent;  
import java.awt.event.ActionListener;  
import java.sql.Connection;  
import java.sql.DriverManager;  
import java.sql.PreparedStatement;  
import java.sql.ResultSet;  
import java.sql.SQLException;
import java.util.*; 

public class Query extends JFrame implements ActionListener{  
	//定义组件  
    JLabel jl2=null;  
    JTextField jtf=null;  
    JButton jb=null;  
    JPanel jp1,jp2=null;  
    JTable jtable=null;  
      
    String name=null;  
    String num=null;  
    DefaultTableModel tableModel = null;
      
    public static void main(String[] args) {  
       Query t = new Query();  
    }     
    //构造函数  
    public Query() {
    	init();
    }
    ObjectTableModel objModel = null;
    public void init(){
        //创建组件  
        jl2=new JLabel("请输入查询条件：");  
        jtf=new JTextField(10);  
        jb=new JButton("查询");  
        //设置监听  
        jb.addActionListener(this);
        this.setBounds(100,100,200,200);//其中200，250表示组件所在的坐标,100,150表示组件
       
        //设置表格  
        final Object[] columnNames = {"序号","数据"};
//        tableModel = new DefaultTableModel(getData(), columnNames);
        objModel = new ObjectTableModel();
        objModel.setObject(getDataByList());
        jtable = new JTable(objModel);//new JTable(getData(), columnNames);
        
//        jtable.getColumnModel().getColumn(1).setPreferredWidth(80);
        
        jp1=new JPanel();  
        jp2=new JPanel();  
  
        jp1.add(jl2);  
        jp1.add(jtf);  
        jp1.add(jb);
        jp1.setBounds(100,100,200,100);
        this.add(jp1);
        JScrollPane pane = new JScrollPane(jtable);
        pane.setPreferredSize(new Dimension(pane.getWidth() - 50, pane.getHeight()/2));
        pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        pane.setBounds(100,150,100,100);
        this.getContentPane().add(pane);
        
        this.setLayout(new GridLayout(2,3));  
        
        this.setTitle("mongoDB");  
        this.setSize(500,800);
        this.setLocation(100, 100);  
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        this.setVisible(true);  
    }
    public Object[][] getData(){
    	java.util.List<String> list = MyMongoDBClient.getList(MyMongoDBClient.getDataBaseByDB(), "suggestion");
        System.out.println("list="+list.size());
        Object[][] rowData =  new Object[list.size()][2];
        for(int i=0;i<list.size();i++){
        	rowData[i] = new Object[]{i+1,list.get(i)};
        }
        return rowData;
    }
    public java.util.List<Object> getDataByList(){
    	java.util.List<String> list1 = MyMongoDBClient.getList(MyMongoDBClient.getDataBaseByDB(), "suggestion");
    	System.out.println("list1="+list1.size());
    	java.util.List<Object> list = new ArrayList<Object>();
    	for(int i=0;i<list1.size();i++){
    		list.add(list1.get(i));
    	}
    	return list;
    }
    public java.util.List<Object> getDataByList1(String params){
    	java.util.List<String> list1 = MyMongoDBClient.find(MyMongoDBClient.getDataBaseByDB(),
    			"suggestion",MyMongoDBClient.getQueryParams(params));
    	System.out.println("list1="+list1.size());
    	java.util.List<Object> list = new ArrayList<Object>();
    	for(int i=0;i<list1.size();i++){
    		list.add(list1.get(i));
    	}
    	return list;
    }
    
    @Override  
    public void actionPerformed(ActionEvent e) {  
        if(e.getActionCommand()=="查询")  {
            try {
            	System.out.println(jtf.getText());
            	objModel.setObject(getDataByList1(jtf.getText()));
            } catch (Exception e1) {  
                e1.printStackTrace();  
            }  
        }  
          
    } 
}
